<template>
  <div class="login-page">
    <van-nav-bar left-arrow
  @click-left="onClickLeft"  title="账号登录"/>
    <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"

    placeholder="请输入账号"
    :rules="[{ required: true, message: '请填写用户名' },
    {pattern:/^\w{5,}/,message:'用户名至少为6位'}]"
  />
  <van-field
    v-model="password"

    name="password"

    placeholder="请输入密码"
    :rules="[{ required: true, message: '请填写密码' },
    {pattern:/^\w{6,}/,message:'密码至少也为6位'}]"
  />
  <div style="margin: 16px;">
    <van-button round block type="info" native-type="submit" v-if="isshow">登录</van-button>
      <!-- <van-button round block type="info" native-type="submit"
      v-else>退出</van-button> -->
  </div>
</van-form>
<router-link  class="link" to="/index">还没有账号,去注册~</router-link>

  </div>
</template>

<script>

import { login } from '@/api/user'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      isshow: true,
      username: '',
      password: ''
    }
  },
  methods: {
    async  onSubmit (values) {
      // console.log(values)
      const { body } = await login(values)
      console.log(body)
      setToken(body.token)// 存储
      this.$toast.success('登录成功')
      this.$router.push('/user')// 成功后跳转到首页
    },
    onClickLeft () {
      this.$router.push('/user')
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #666;
  font-size: 12px;
  padding-right: 20px;
  float: right;
margin-right: 110px;

}
</style>
